package com.prs.crm.action.hr;

import java.util.Calendar;
import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;

import com.prs.crm.context.DataBuilder;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.hr.SalaryFloatItem;
import com.prs.crm.domain.hr.SalaryItem;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.hr.SalaryFloatItemService;
import com.prs.crm.service.hr.SalaryItemService;

public class PaySetAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	
	private PersonService personService;
	
	private SalaryItemService salaryItemService;
	
	private SalaryFloatItemService salaryFloatItemService;
	
	private Collection<Integer> years;
	
	private Collection<Integer> months;
	
	private Collection<SalaryItem> items;
	
	private Collection<SalaryFloatItem> floatItems;
	
	private String username;
	
	private Boolean fuzzy;
	
	/**
	 * 薪酬设置的年份
	 */
	private Integer year;
	
	/**
	 * 薪酬设置的月份
	 */
	private Integer month;
	
	private Person person;
	
	private Integer[] itemIds;//基础薪资设置,与 itemValues一 一对应 
	
	private Integer[] floatItemIds;//浮动基础薪资设置,与 floatitemValues一 一对应
	
	private String[] itemValues;
	
	private String[] floatItemValues;

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public PersonService getPersonService() {
		return personService;
	}

	public void setSalaryItemService(SalaryItemService salaryItemService) {
		this.salaryItemService = salaryItemService;
	}

	public SalaryItemService getSalaryItemService() {
		return salaryItemService;
	}

	public void setSalaryFloatItemService(SalaryFloatItemService salaryFloatItemService) {
		this.salaryFloatItemService = salaryFloatItemService;
	}

	public SalaryFloatItemService getSalaryFloatItemService() {
		return salaryFloatItemService;
	}

	public void setYears(Collection<Integer> years) {
		this.years = years;
	}

	public Collection<Integer> getYears() {
		return years;
	}

	public void setMonths(Collection<Integer> months) {
		this.months = months;
	}

	public Collection<Integer> getMonths() {
		return months;
	}

	public void setItems(Collection<SalaryItem> items) {
		this.items = items;
	}

	public Collection<SalaryItem> getItems() {
		return items;
	}

	public void setFuzzy(Boolean fuzzy) {
		this.fuzzy = fuzzy;
	}

	public Boolean getFuzzy() {
		return fuzzy;
	}

	public void setFloatItems(Collection<SalaryFloatItem> floatItems) {
		this.floatItems = floatItems;
	}

	public Collection<SalaryFloatItem> getFloatItems() {
		return floatItems;
	}

	public void setYear(Integer year) {
		this.year = year;
	}

	public Integer getYear() {
		return year;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUsername() {
		return username;
	}

	public void setMonth(Integer month) {
		this.month = month;
	}

	public Integer getMonth() {
		return month;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public Person getPerson() {
		return person;
	}
	
	
	public void setItemIds(Integer[] itemIds) {
		this.itemIds = itemIds;
	}

	public Integer[] getItemIds() {
		return itemIds;
	}

	public void setFloatItemIds(Integer[] floatItemIds) {
		this.floatItemIds = floatItemIds;
	}

	public Integer[] getFloatItemIds() {
		return floatItemIds;
	}

	public void setItemValues(String[] itemValues) {
		this.itemValues = itemValues;
	}

	public String[] getItemValues() {
		return itemValues;
	}

	public void setFloatItemValues(String[] floatItemValues) {
		this.floatItemValues = floatItemValues;
	}

	public String[] getFloatItemValues() {
		return floatItemValues;
	}

	/**
	 * 准备薪资设置
	 * @return
	 */
	@Action("preparePay")
	public String preparePay()
	{
		Calendar c = Calendar.getInstance(); 
		c.setTime(new java.util.Date()); 
		this.setMonths(DataBuilder.buildMonthArea());
		this.setYears(DataBuilder.buildYearArea(2, 10));
		if (year==null)
		{
			this.setYear(c.get(Calendar.YEAR));
		}
		if (month==null)
		{
			this.setMonth(c.get(Calendar.MONTH));
		}
		return SUCCESS;
	}
	
	
	/**
	 * 开始设置
	 * @return
	 */
	@Action(value="startPay",results={@Result(name="success",type="redirect",location="paySet?person=${person.id}&year=${year}&month=${month}")
							,@Result(name="error",type="chain",location="preparePay")})
	public String startPay()
	{
		if (username!=null && username.trim().length()>0)
		{
			this.setPerson(this.getPersonService().findByUserName(username, fuzzy));
		}
		else
		{
			this.setPerson(this.getPersonService().findFirstPerson());
		}
		if (person==null)
		{
			this.setPrompt("没有搜索到适合条件的人员,请重新设置搜索条件");
			return ERROR;
		}
		return SUCCESS;
	}
	
	@Action(value="prevPay",results={@Result(name="success",type="redirect",location="paySet?person=${person.id}&year=${year}&month=${month}")})
	public String prevPay()
	{
		Person prevperson = this.getPersonService().findPrev(person);
		if (prevperson!=null)
			this.setPerson(prevperson);
		return SUCCESS;
	}
	
	@Action(value="nextPay",results={@Result(name="success",type="redirect",location="paySet?person=${person.id}&year=${year}&month=${month}")})
	public String nextPay()
	{
		Person nextperson = this.getPersonService().findNext(person);
		if (nextperson!=null)
			this.setPerson(nextperson);
		return SUCCESS;
	}
	/**
	 * 设置
	 * @return
	 */
	@Action("paySet")
	public String paySet()
	{
		/**
		 * 如果针对该人员的薪酬 已经设置，则需要取出结果
		 */
		this.setFloatItems(this.getSalaryFloatItemService().getAllSalaryFloatItem());
		this.setItems(this.getSalaryItemService().getAllSalaryItems());
		return SUCCESS;
	}
	
	/**
	 * 保存设置
	 */
	@Action(value="savePay",results={@Result(name="success",type="redirect",location="paySet?person=${person.id}&year=${year}&month=${month}")
			,@Result(name="error",location="/hr/paySetEnd.ftl")})
	public String savePay()
	{
		/**
		 * -----这里是保存设置,
		 * 保存的数据
		 * itemIds 存放的是薪资ID，与itemValues一一对应
		 * floatItemIds 存放的是浮动薪资ID，与floatItemValues一一对应
		 */
		this.setPerson(this.getPersonService().findNext(person));
		if (this.getPerson()==null)
		{
			this.setPrompt("薪酬设置完成,请返回");
			return ERROR;
		}
		return SUCCESS;
	}

}
